package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.Region;
import com.ruoyi.nongjt.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 区域管理Controller
 *
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/nongjt/region")
public class NjtRegionController extends BaseController
{
    @Autowired
    private RegionService regionService;

    /**
     * 查询区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(Region region)
    {
        startPage();
        List<Region> list = regionService.selectRegionList(region);
        return getDataTable(list);
    }

    /**
     * 查询根节点列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:list')")
    @GetMapping("/root")
    public AjaxResult getRootRegions()
    {
        List<Region> list = regionService.selectRootRegionList();
        return success(list);
    }

    /**
     * 根据父级ID查询子节点列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:list')")
    @GetMapping("/children/{parentId}")
    public AjaxResult getChildrenRegions(@PathVariable("parentId") Long parentId)
    {
        List<Region> list = regionService.selectChildrenByParentId(parentId);
        return success(list);
    }

    /**
     * 导出区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:export')")
    @Log(title = "区域管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        util.exportExcel(response, list, "区域管理数据");
    }

    /**
     * 获取区域管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(regionService.selectRegionById(id));
    }

    /**
     * 新增区域管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:add')")
    @Log(title = "区域管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Region region)
    {
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改区域管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:edit')")
    @Log(title = "区域管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Region region)
    {
        return toAjax(regionService.updateRegion(region));
    }

    /**
     * 删除区域管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:remove')")
    @Log(title = "区域管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(regionService.deleteRegionByIds(ids));
    }

    /**
     * 递归删除区域管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:remove')")
    @Log(title = "区域管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/recursive/{id}")
    public AjaxResult removeRecursively(@PathVariable("id") Long id)
    {
        return toAjax(regionService.deleteRegionRecursively(id));
    }

    /**
     * 检查是否有子节点
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:query')")
    @GetMapping("/checkChildren/{id}")
    public AjaxResult checkHasChildren(@PathVariable("id") Long id)
    {
        boolean hasChildren = regionService.checkHasChildren(id);
        return success(hasChildren);
    }

    /**
     * 获取区域类型选项
     */
    @PreAuthorize("@ss.hasPermi('nongjt:region:query')")
    @GetMapping("/regionTypes")
    public AjaxResult getRegionTypes()
    {
        List<String> regionTypes = regionService.getRegionTypeOptions();
        return success(regionTypes);
    }
}